<?php
namespace backend\controllers;

use backend\models\NewProject;
use backend\models\ProjectActor;
use backend\models\ProjectClass;
use backend\models\ProjectPicture;
use backend\models\ProjectType;
use backend\models\ProjectVideo;
use common\components\BaseController;
use Yii;
use \common\helps\tools;
use \common\helps\code;
use yii\db\Query;

/**
 * AuthRule controller
 */
class ProjectController extends BaseController
{

    public $modelClass = 'backend\models\NewProject';
    public function init()
    {
//        $this->enableAuth=false;
        parent::init();
    }


    /***项目分类相关***/
    /**
     * 获取项目分类列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetProjectClassList(){
        $request =$this->request;
        $model = new ProjectClass();
        $params = $request->queryParams;
        $total_data = $model->getProjectClassList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','名称','状态'];
            foreach($total_data as &$value){
                if($value['status']>1){
                    $value['status'] ='停用';
                }else{
                    $value['status'] ='启用';
                }
            }
            tools::ToPHPEXECL($tableHeader,$total_data,'项目分类列表');
        }


        return code::success($data);

    }

    /**
     * 停启用项目分类
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectClassState(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectClass();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //停启用
                $model->scenario='update_status';
            }else{
                return code::error('','参数错误');
            }

            $model->load($params,'');
            if(!$model->validate()){
                $error = array_values($model->getErrors());
                return code::error('',$error[0][0]);
            }
            $data=$model->editProjectClassState($params);
            if(!empty($data['error'])){
                return code::error('',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 删除项目分类
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDelProjectClassState(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectClass();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //删除
                $model->scenario='update_status';
            }else{
                return code::error('','参数错误');
            }
            $model->load($params,'');
            if(!$model->validate()){
                $error = array_values($model->getErrors());
                return code::error('',$error[0][0]);
            }
            $data=$model->editProjectClassState($params);
            if(!empty($data['error'])){
                return code::error('',$data['error']);
            }else{
                return code::success();
            }
        }
    }



    /**
     * 编辑项目分类
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectClassInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectClass();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //编辑
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectClassInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }
        }
    }

    /**
     * 新增项目分类
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddProjectClassInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectClass();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //新增
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectClassInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }



    /***项目类型相关***/
    /**
     * 获取项目类型列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetProjectTypeList(){
        $request =$this->request;
        $model = new ProjectType();
        $params = $request->queryParams;
        $total_data = $model->getProjectTypeList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','名称','状态'];
            foreach($total_data as &$value){
                if($value['status']>1){
                    $value['status'] ='停用';
                }else{
                    $value['status'] ='启用';
                }
            }
            tools::ToPHPEXECL($tableHeader,$total_data,'项目类型列表');
        }


        return code::success($data);

    }

    /**
     * 停启用项目类型
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectTypeState(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectType();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //停启用
                $model->scenario='update_status';
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectTypeState($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }
        }
    }

    /**
     * 删除项目类型
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDelProjectTypeState(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectType();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //删除
                $model->scenario='update_status';
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectTypeState($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }



    /**
     * 编辑项目类型
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectTypeInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectType();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //编辑
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectTypeInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }


        }
    }


    /**
     * 新增项目类型
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddProjectTypeInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectType();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //新增
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectTypeInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }
        }
    }


    /***项目列表相关***/
    /**
     * 获取项目类型列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetProjectList(){
        $request =$this->request;
        $model = new NewProject();
        $params = $request->queryParams;
        $total_data = $model->getProjectList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        if(!empty($params['excel_download']) && $params['excel_download']>0){
            $tableHeader = ['id','缩略图','名称','分类','发布人','推荐','创建时间'];
            foreach($total_data as &$value){
                if($value['is_recommend']>1){
                    $value['is_recommend'] ='否';
                }else{
                    $value['is_recommend'] ='是';
                }
            }
            tools::ToPHPEXECL($tableHeader,$total_data,'项目列表');
        }


        return code::success($data);

    }


    /**
     * 停启用项目
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectState(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new NewProject();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //停启用
                $model->scenario='update_status';
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectState($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }

    /**
     * 删除项目
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDelProjectState(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new NewProject();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //删除
                $model->scenario='update_status';
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectState($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }
        }
    }


    /**
     * 获取项目详情
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetProjectInfo(){
        $request =$this->request;
        $params = $request->queryParams;
        if(empty($params['id'])){  return code::error('','参数错误');}
        $data = $list = (new Query())
            ->select('new_project.*,project_class.title as class_title')
            ->from('new_project')
            ->innerJoin('project_class','project_class.id=new_project.class_id')
            ->where(['new_project.id'=>intval(trim($params['id']))])
            ->one();
        if(!empty($data)){
            $data['tags'] = unserialize($data['tags']);
            return code::success($data);
        }else{
            return code::error('','未查询到相关数据');
        }

    }


    /**
     * 编辑项目
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new NewProject();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //编辑
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $params['uid'] = $this->user_id;
                $data=$model->editProjectInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }


    /**
     * 新增项目
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddProjectInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new NewProject();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //新增
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $params['uid'] = $this->user_id;
                $data=$model->editProjectInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }


        }
    }

    /**
     * 编辑项目分析信息
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectOtherInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new NewProject();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==3){ //编辑项目分析信息
                $model->scenario='update_project_analyse';
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $params['uid'] = $this->user_id;
                $data=$model->editProjectInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }




    /***演职人员相关***/
    /**
     * 获取演职人员列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetProjectActorList(){
        $request =$this->request;
        $model = new ProjectActor();
        $params = $request->queryParams;
        if(empty($params['project_id'])) return code::error('','项目参数错误');
        $total_data = $model->getProjectActorList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        return code::success($data);

    }

    /**
     * 根据名称获取演职人员信息
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetProjectActorInfo(){
        $request =$this->request;
        $model = new ProjectActor();
        $params = $request->queryParams;
        $total_data = $model->getProjectActorinfo($params);
        return code::success($total_data);

    }



    /**
     * 删除演职人员
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionDelProjectActor(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectActor();
            $params = $request->bodyParams;
            if(empty($params['id'])){ //删除
                return code::error('','参数错误');
            }
            $data=$model->delProjectActor($params);
            if(!empty($data['error'])){
                return code::error('',$data['error']);
            }else{
                return code::success();
            }
        }
    }

    /**
     * 编辑演职人员
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectActorInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectActor();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //编辑
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectActorInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }


        }
    }

    /**
     * 新增演职人员
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddProjectActorInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectActor();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //新增
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $data=$model->editProjectActorInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }



    /***项目视频相关***/
    /**
     * 获取演职人员列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetProjectVideoList(){
        $request =$this->request;
        $model = new ProjectVideo();
        $params = $request->queryParams;
        if(empty($params['project_id'])) return code::error('','项目参数错误');
        $total_data = $model->getProjectVideoList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        return code::success($data);

    }


    /**
     * 删除项目视频
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectVideoInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectVideo();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //删除
                $model->scenario='pre_update';
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $params['uid'] = $this->user_id;
                $data=$model->editProjectVideoInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }


    /**
     * 新增项目视频
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddProjectVideoInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectVideo();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //新增
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $params['uid'] = $this->user_id;
                $data=$model->editProjectVideoInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }
        }
    }


    /**
     * 获取项目海报列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetProjectPictureList(){
        $request =$this->request;
        $model = new ProjectPicture();
        $params = $request->queryParams;
        if(empty($params['project_id'])) return code::error('','项目参数错误');
        $total_data = $model->getProjectPictureList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);
        return code::success($data);

    }

    /**
     * 新增项目海报
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionAddProjectPictureInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectPicture();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==1){ //新增

                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $params['uid'] = $this->user_id;
                $data=$model->editProjectPictureInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success(['id'=>$data['id']]);
                }
            }else{
                return code::error('','参数错误');
            }

        }
    }

    /**
     * 删除项目海报
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionUpdateProjectPictureInfo(){
        if($this->request->isPost) {
            $request =$this->request;
            $model = new ProjectPicture();
            $params = $request->bodyParams;
            if(!empty($params['type']) && $params['type']==2){ //删除
                $model->scenario='pre_update';
                $model->load($params,'');
                if(!$model->validate()){
                    $error = array_values($model->getErrors());
                    return code::error('',$error[0][0]);
                }
                $params['uid'] = $this->user_id;
                $data=$model->editProjectPictureInfo($params);
                if(!empty($data['error'])){
                    return code::error('',$data['error']);
                }else{
                    return code::success();
                }
            }else{
                return code::error('','参数错误');
            }
        }
    }



}
